<?php

App::uses('AppController', 'Controller');

class AuthController extends AppController {

	public $uses = array('Member');

	public function authorization() {
		
		$this->layout = 'sub_template';
		$this->render('/Auth/index');
	}

	function afterFilter()
	{
		if($this->Session->check('User')) {
			$this->redirect(array('controller' => 'index', 'action' => 'index'));
		}
	}
	
	public function login() {

		if(empty($this->data['Member']['email']) == false)
		{
			// Here we validate the user by calling that method from the User model
			if(($user = $this->Member->validateLogin($this->data['Member'])) != null)
			{
			
				// Write some Session variables and redirect to our next page!
				$this->Session->write('User', $user);

				// Go to our first destination!
				$this->Redirect(array('controller' => 'category', 'action' => 'index'));
					
			} else {
				$this->Redirect('/auth');
			}
		}
	}

	public function logout() {

		$this->Session->destroy();

		// Go home!
		$this->Redirect('/');
	}
}
